from datetime import date, datetime, time, timedelta
from typing import List

from sqlalchemy.orm import Session

from module_admin.dao.cms_car_dao import CmsCarDao
from module_admin.dao.cms_order_dao import CmsOrderDao
from module_admin.dao.cms_order_dispatch_dao import CmsOrderDispatchDao
from module_admin.dao.cms_order_execute_dao import CmsOrderExecuteDao
from module_admin.entity.do.cms_car import CmsCar
from module_admin.entity.do.cms_order import CmsOrder
from module_admin.entity.vo.cms_car_vo import CarModel, CrudCarResponse, CarForQuery, CarModelForPage, CarOrderList, \
    ArchivesMessage, CarModelAndDetails, OrderListMessageForQuery, CarPageObject
from module_admin.entity.vo.cms_driver_vo import OrderListForQueryAfter, OrderListForQuery
from utils.stringUtils import stringUtils
from utils.time_format_util import list_format_datetime
from utils.uuid import UUID


class CmsCarService:

    @classmethod
    def create_car(cls, result_db: Session, page_object: CarModel):
        # 待审核
        page_object.state = 1
        uuid = UUID().uuid()
        page_object.id = int(uuid)
        page_object.car_id = "car_" + uuid
        try:
            CmsCarDao.create_car(result_db, page_object)
            result_db.commit()
            result = dict(is_success=True, message='新增成功')
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))

        return CrudCarResponse(**result)

    @classmethod
    def delete_car(cls, query_db: Session, car_id: int):
        CmsCarDao.delete_car_by_id(query_db, car_id)
        query_db.commit()

    @classmethod
    def update_car(cls, db, car_update: CarModel):
        car_update_dict = car_update.dict(exclude_unset=True)
        try:
            CmsCarDao.update_car_by_id(db, car_update_dict)
            db.commit()
            result = dict(is_success=True, message='更新成功')
        except Exception as e:
            db.rollback()
            result = dict(is_success=False, message=str(e))
        return CrudCarResponse(**result)

    @classmethod
    def get_car(cls, query_db: Session, id: int):
        car_by_id: CmsCar = CmsCarDao.get_car_by_id(query_db, id)
        car_by_id_dict = car_by_id.__dict__
        return CarModel(**car_by_id_dict)

    @classmethod
    def list(cls, db: Session, query_object: CarForQuery):
        car_list_result = CmsCarDao.list(db, query_object)
        return car_list_result

    @classmethod
    def page(cls, db, car_page_query: CarPageObject):
        car_list_result = CmsCarDao.page(db, car_page_query)
        return car_list_result

    @classmethod
    def count(cls, db, car_page_query: CarPageObject):
        car_list_result = CmsCarDao.count(db, car_page_query)
        return car_list_result

    @classmethod
    def list_all(cls, db, order_id_num):
        # 单号查开始结束时间
        order: CmsOrder = CmsOrderDao.get_order_by_id(db, order_id_num)
        car_list_result = None
        if order is None:
            car_list_result = CmsCarDao.list_all(db, None, None)
        else:
            car_list_result = CmsCarDao.list_all(db, order.plan_begin_time, order.plan_end_time)
        return car_list_result

    @classmethod
    def list_all_for_urgent(cls, db, order_plan_time):
        return CmsCarDao.list_all(db, order_plan_time.plan_begin_time, order_plan_time.plan_end_time)

    @classmethod
    def list_free(cls, db: Session, order_id: int):
        # 单号查开始结束时间
        order: CmsOrder = CmsOrderDao.get_order_by_id(db, order_id)
        if order is None:
            return None
        car_list_result = CmsCarDao.list_free(db, order.plan_begin_time, order.plan_end_time)
        return car_list_result

    @classmethod
    def list_free_for_urgent(cls, db, order_plan_time):
        return CmsCarDao.list_free(db, order_plan_time.plan_begin_time, order_plan_time.plan_end_time)

    @classmethod
    def order_list(cls, db, car_order_query: OrderListForQuery):
        # 处理时间
        after = OrderListForQueryAfter()
        after.query_begin = car_order_query.start_time
        after.query_end = car_order_query.end_time
        list0 = []
        query = CarForQuery()
        driver_list_result: List[CmsCar] = CmsCarDao.list(db, query)
        for e in driver_list_result:
            car_order_list = CarOrderList()
            car_order_list.id = e.id
            car_order_list.car_id = e.car_id
            car_order_list.car_number = e.car_number
            after.car_id = e.car_id
            order_list_result = CmsOrderDispatchDao.list_by_driver_or_car(db, after)
            list_format_datetime(order_list_result)
            car_order_list.order_list = order_list_result
            if order_list_result:
                list0.append(car_order_list)
        return list0

    @classmethod
    def get_car_new(cls, db, id):
        details = CarModelAndDetails()
        busy = CmsCarDao.is_busy(db, id)
        # 基本信息
        archives_message: ArchivesMessage = CmsCarDao.get_car_details_by_id(db, id)
        details.id = archives_message.id
        details.car_id = archives_message.car_id
        details.car_number = archives_message.car_number
        details.car_device_no = archives_message.car_device_no
        details.car_state = "任务进行中" if busy else "空闲"
        # 档案信息
        details.archives_message = archives_message
        # 实时信息
        real_time_message = CmsCarDao.get_car_order_by_id(db, id)
        details.real_time_message = real_time_message
        # 统计信息
        stats_message = CmsCarDao.get_car_sum(db, id)
        details.stats_message = stats_message
        return details

    @classmethod
    def get_car_new_order_list(cls, db, order: OrderListMessageForQuery):
        list_by_time = CmsOrderExecuteDao.get_list_by_time(db,order)
        return list_by_time
